package com.heima.wemedia.aip;

import com.heima.file.service.FileStorageService;
import com.heima.utils.baidu.Base64Util;
import com.heima.utils.baidu.HttpUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.net.URLEncoder;

/**
* 图像审核接口
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ImgCensor {

    @Autowired
    private FileStorageService fileStorageService;

    @Test
    public void ImgCensor() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined";
        try {
            // 本地文件路径
            byte[] imgData = fileStorageService.downLoadFile("http://192.168.200.130:9000/leadnews/2023/06/09/55c47614-46bb-4b86-9ddc-7924a39070f9.png");

            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.8f9d4404a7abffe7922867bde24bbf32.2592000.1688989096.282335-34643057";

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}